using System;
using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using mas.Models;

namespace mas.ViewModels;

public partial class ImageDetailViewModel : ViewModelBase, IDisposable
{
    private readonly HeaderData _headerData;  // 持有原始HeaderData引用
    
    [ObservableProperty] private Bitmap? _image;

    public ImageDetailViewModel(HeaderData headerData)
    {
        _headerData = headerData;
        _image = headerData.Image;
        
        // 监听原始数据变化
        _headerData.PropertyChanged += OnHeaderDataPropertyChanged;
    }

    // 响应HeaderData属性变化
    private void OnHeaderDataPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(HeaderData.Image))
        {
            Image = _headerData.Image;  // 更新图像
        }
    }

    // 释放资源（取消订阅避免内存泄漏）
    public void Dispose()
    {
        _headerData.PropertyChanged -= OnHeaderDataPropertyChanged;
    }
}